// pages/dish/pay/pay.js
const server = require('../../../utils/server');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList: [],
    showDialog: false,
    currentCoupon: null,
    couponList: null,
    storeName: null,
    totalNumber: 0,
    totalPrice: 0,
    address:null
  },

  goCoupon() {
    wx.navigateTo({
      url: '/pages/dish/coupon/coupon',
    })
  },

  getCouponLength() {
    let that = this;
    server.requestForm(true, 'get', 'coupon', {}, function (res) {
      if (res.code == 0) {
        that.setData({
          couponList: res.data
        })
      }
    });
  },

  addNumber(e) {
    let index = e.currentTarget.dataset.index;
    let list = this.data.cartList;
    list[index].salesNumber++;
    this.setData({
      cartList: list
    });
    this.getTotal(list)
  },

  lessNumber(e) {
    let index = e.currentTarget.dataset.index;
    let list = this.data.cartList;
    let that = this;
    if (list[index].salesNumber == 1) {
      wx.showModal({
        content: "确定删除此商品吗？",
        cancelText: "取消",
        confirmText: "确认",
        success(res) {
          if (res.confirm) {
            list.splice(index, 1);
          } else {
            return
          };
          that.setData({
            cartList: list
          });
          that.getTotal(list);
        }
      })
    } else {
      list[index].salesNumber--
    }
    that.setData({
      cartList: list
    });
    that.getTotal(list);
  },

  getTotal(list) {
    let totalPrice = 0;
    let totalNumber = 0;
    if (list.length) {
      list.forEach((item, index) => {
        totalPrice += item.salePrice * item.salesNumber;
        totalNumber += item.salesNumber
      })
    };
    this.setData({
      totalPrice: totalPrice,
      totalNumber: totalNumber
    })
  },

  getPay(price, coupon) {
    let final = price - coupon;
    this.setData({
      totalPrice: final
    })
  },

  getAddress(){
    let that=this;
    server.requestForm(true,'get','address',{},function(res){
      if(res.code ==0 ){
        let list=res.data;
        list.forEach((item,index)=>{
          if(item.defaultBool == true){
            console.log(item)
            that.setData({
              address:item
            })
          }
        })
      }
    })
  },

  goAddress(){
    wx.navigateTo({
      url: '/pages/dish/address/address',
    })
  },

  gopay(){
    wx.requestPayment({
      nonceStr: 'nonceStr',
      package: 'package',
      paySign: 'paySign',
      timeStamp: 'timeStamp',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCouponLength();
    this.getAddress();
    let list = wx.getStorageSync('cartList');
    this.getTotal(list);
    let storeName = wx.getStorageSync('storeName')
    this.setData({
      cartList: list,
      storeName: storeName
    })
    if (options.id) {
      let couponId = options.id;
      let couponValue = options.value;
      let price = this.data.totalPrice;
      this.setData({
        currentCoupon: couponValue
      });
      this.getPay(price, couponValue)
    };
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  getCart() {
    server.requestForm(true, "get", "cart", {});
  }
})